#include "Client.h"
#include "ClientConnect.h"
#include "ThreadListen.h"

ThreadListen::ThreadListen(Client* cli, ClientConnect* con) : ClientThread(cli, "ThreadListen") {
	client = cli;
	connect = con;
	ticks = 0;
	loginCmd = false;
}

u8 ThreadListen::GetLastCmd(){
	return lastCmd;
}

bool ThreadListen::GetLoginCmd(){
	return loginCmd;
}

void ThreadListen::run(){
	connect->setReceiving(true);
	u8 cmd = connect->ReadByte();
	printf("Received command id : 0x%02X\n", cmd); fflush(stdout);

	if(ticks == 0){
		geSleep(500);
	}

	switch(cmd){
		case 0x00: connect->GetKeepAlive(); break;
		case 0x01: connect->GetLogin(); break;
		case 0x03: connect->GetMessage(); break;
		case 0x04: connect->TimeUpdate(); break;
		case 0x05: connect->EntityEquipment(); break;
		case 0x06: connect->SpawnPosition(); break;
		case 0x08: connect->UpdateHealth(); break;
		case 0x0D: connect->GetPlayerPositionLook(); break;
		case 0x10: connect->GetHeldItemChange(); break;
		case 0x14: connect->SpawnNamedEntity(); break;
		case 0x17: connect->SpawnObjectVehicle(); break;
		case 0x18: connect->SpawnMob(); break;
		case 0x19: connect->SpawnPainting(); break;
		case 0x1C: connect->EntityVelocity(); break;
		case 0x1D: connect->DestroyEntity(); break;
		case 0x1F: connect->EntityRelativeMove(); break;
		case 0x20: connect->EntityLook(); break;
		case 0x21: connect->EntityLookandRelativeMove(); break;
		case 0x22: connect->EntityTeleport(); break;
		case 0x23: connect->EntityHeadLook(); break;
		case 0x26: connect->EntityStatus(); break;
		case 0x28: connect->EntityMetadata(); break;
		case 0x2B: connect->SetExperience(); break;
		case 0x33: connect->ChunkData(); break;
		case 0x34: connect->MultiBlockChange(); break;
		case 0x35: connect->BlockChange(); break;
		case 0x36: connect->BlockAction(); break;
		case 0x38: connect->MapChunkBulk(); break;
		case 0x3E: connect->NamedSoundEffect(); break;
		case 0x46: connect->ChangeGameState(); break;
		case 0x67: connect->GetSlot(); break;
		case 0x68: connect->GetWindowItems(); break;
		case 0x82: connect->GetUpdateSign(); break;
		case 0x84: connect->UpdateTileEntity(); break;
		case 0xC9: connect->PlayerListItem(); break;
		case 0xCA: connect->GetPlayerAbilities(); break;
		case 0xFA: connect->GetPluginMessage(); break;
		case 0xFF: connect->ServerError(); break;
		default :
			gePrintDebug(0x101, "Unknown CMD 0x%02X !\n", cmd);
			while(1)geSleep(1);
			break;
	}

	lastCmd = cmd;
	if(cmd == 0x01){
		loginCmd = true;
	}

	connect->setReceiving(false);
	ticks = geWaitTick(2, ticks);
}
